﻿Ext.grid.CheckColumn = function(config){
    Ext.apply(this, config);
    if(!this.id){
        this.id = Ext.id();
    }
    this.renderer = this.renderer.createDelegate(this);
};
Ext.extend(Ext.grid.CheckColumn, Ext.util.Observable, {
    init : function(grid){
        this.grid = grid;
        this.grid.on('render', function(){
            var view = this.grid.getView();
            view.mainBody.on('mousedown', this.onMouseDown, this);
        }, this);
    },

    onMouseDown : function(e, t){
        if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
            e.stopEvent();
            var row = this.grid.getView().findRowIndex(t);
            var col = this.grid.getView().findCellIndex(t);
            var record = this.grid.store.getAt(row);
            var field = this.dataIndex;
            var startValue = record.data[field];
            var value = !startValue;            
            
	        if(String(value) !== String(startValue)){
	            var e = {
	                grid: this.grid,
	                record: record,
	                field: field,
	                originalValue: startValue,
	                value: value,
	                row: row,
	                column: col,
	                cancel:false
	            };
	            if(this.grid.fireEvent("validateedit", e) !== false && !e.cancel){
	                record.set(field, value);
	                delete e.cancel;
	                this.grid.fireEvent("afteredit", e);
	            }
	        }
        }
    },

    renderer : function(v, p, record){
        p.css += ' x-grid3-check-col-td'; 
        return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'">&#160;</div>';
    }
});